//
//  LSYFileUtil.m
//  LSYReader
//
//  Created by sam on 2018/3/26.
//  Copyright © 2018年 okwei. All rights reserved.
//
#import "NSString+EV.h"
#import "LSYFileUtil.h"

@implementation LSYFileUtil

+ (NSString *)documentPath {
    NSArray* paths = NSSearchPathForDirectoriesInDomains(NSDocumentDirectory, NSUserDomainMask, YES);
    NSString* path = paths[0];
    return path;
}
+ (NSString *)netBookDir {
    NSString *path = [[self class] documentPath];
    NSString* bookDir = [path stringByAppendingPathComponent:@"netbook"];
    BOOL isDirectory = YES;
    if ([[NSFileManager defaultManager] fileExistsAtPath:bookDir isDirectory:&isDirectory]){
        return bookDir;
    } else {
        if ([[NSFileManager defaultManager] createDirectoryAtPath:bookDir withIntermediateDirectories:YES attributes:nil error:nil]) {
            return bookDir;
        } else {
            return path;
        }
    }
}
+ (NSString *)bookDir:(NSString *)bookName ruleHost:(NSString *)ruleHost {
    NSString *bookDir = [NSString stringWithFormat:@"%@/%@",bookName.ev_MD5,ruleHost.ev_MD5];
    NSString *bookPath = [[LSYFileUtil netBookDir] stringByAppendingPathComponent:bookDir];
    BOOL isDirectory = YES;
    NSString *netBookDir = [NSString stringWithFormat:@"netbook/%@",bookDir];
    if ([[NSFileManager defaultManager] fileExistsAtPath:bookPath isDirectory:&isDirectory]) {
        return netBookDir;
    } else {
        if ([[NSFileManager defaultManager] createDirectoryAtPath:bookPath withIntermediateDirectories:YES attributes:nil error:nil]) {
            return netBookDir;
        }
        return netBookDir;
    }
}
+(NSString *)imageDir:(LSYBook *)book {
    return [[self class] bookDir:book.bookName ruleHost:book.rule.host];
}

+ (NSString *)bookPath:(LSYBook *)book {
    NSString *bookDir = [[self class] bookDir:book.bookName ruleHost:book.rule.host];
    return [NSString stringWithFormat:@"%@/%@_book",bookDir,book.bookName];
}

+(NSString *)absBookPath:(LSYBook *)book {
    return [[self class] absolutePath:[[self class] bookPath:book]];
}

+ (NSString *)chapterPath:(LSYChapter *)chapter {
    NSString *bookDir = [[self class] bookDir:chapter.bookName ruleHost:chapter.ruleHost];
    return [NSString stringWithFormat:@"%@/%@",bookDir,chapter.chapterName.ev_MD5];
}
+(NSString *)absChapterPath:(LSYChapter *)chapter {
    return [[self class] absolutePath:[[self class] chapterPath:chapter]];
}

+(NSString *)bookStoreDir {
    NSString* path = [[self class] documentPath];
    NSString* bookDir = [path stringByAppendingPathComponent:@"bookstore"];
    BOOL isDirectory = YES;
    if ([[NSFileManager defaultManager] fileExistsAtPath:bookDir isDirectory:&isDirectory]){
        return bookDir;
    } else {
        if ([[NSFileManager defaultManager] createDirectoryAtPath:bookDir withIntermediateDirectories:YES attributes:nil error:nil]) {
            return bookDir;
        } else {
            return path;
        }
    }
}

+(NSString *)bookStorePath {
    NSString *bookStoreDir = [[self class] bookStoreDir];
    return [bookStoreDir stringByAppendingPathComponent:@"books.zip"];
}

+(NSString *)ruleStorePath {
    NSString *bookStoreDir = [[self class] bookStoreDir];
    return [bookStoreDir stringByAppendingPathComponent:@"rules.zip"];
}

+(NSString *)absolutePath:(NSString *)path{
    return [NSString stringWithFormat:@"%@/%@",[[self class] documentPath],path];
}

+(BOOL)removePath:(NSString *)path {
    NSError *err;
    if ([[NSFileManager defaultManager] removeItemAtPath:path error:&err]) {
        return YES;
    }
    return NO;
}

+(BOOL)removeURL:(NSURL *)fileUrl{
    NSError *err;
    if ([[NSFileManager defaultManager] removeItemAtURL:fileUrl error:&err]) {
        return YES;
    }
    return NO;
}

@end
